﻿/*========================== 
 * @author 郑金泉
 * @desc SQL LIKE 条件类
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;

namespace Obsidian.Data.Sql
{
    public class LikeCondition : SqlCondition, ISqlCondition
    {

        private ModelField _modelField;
        protected LikeClauseKeyWord _clauseKeyWord = LikeClauseKeyWord.LIKE;
        private string _likeKeyWord = null;
        private LikeType _likeType = LikeType.Both;

        public LikeCondition(ModelField field, string likeKeyWord)
        {
            this.Init(Sql.LogicalOperator.AND, field, LikeClauseKeyWord.LIKE, likeKeyWord, LikeType.Both);
        }
        public LikeCondition(ModelField field, LikeClauseKeyWord clauseKeyWord, string likeKeyWord)
        {
            this.Init(Sql.LogicalOperator.AND, field, clauseKeyWord, likeKeyWord, LikeType.Both);
        }

        public LikeCondition(Sql.LogicalOperator logicalOperator, ModelField field, string likeKeyWord)
        {
            this.Init(logicalOperator, field, LikeClauseKeyWord.LIKE, likeKeyWord, LikeType.Both);
        }
        public LikeCondition(Sql.LogicalOperator logicalOperator, ModelField field, LikeClauseKeyWord clauseKeyWord, string likeKeyWord)
        {
            this.Init(logicalOperator, field, clauseKeyWord, likeKeyWord, LikeType.Both);
        }

        public LikeCondition(ModelField field, string likeKeyWord, LikeType likeType)
        {
            this.Init(Sql.LogicalOperator.AND, field, LikeClauseKeyWord.LIKE, likeKeyWord, likeType);
        }
        public LikeCondition(ModelField field, LikeClauseKeyWord clauseKeyWord, string likeKeyWord, LikeType likeType)
        {
            this.Init(Sql.LogicalOperator.AND, field, clauseKeyWord, likeKeyWord, likeType);
        }

        public LikeCondition(Sql.LogicalOperator logicalOperator, ModelField field, string likeKeyWord, LikeType likeType)
        {
            this.Init(logicalOperator, field, LikeClauseKeyWord.LIKE, likeKeyWord, likeType);
        }
        public LikeCondition(Sql.LogicalOperator logicalOperator, ModelField field, LikeClauseKeyWord clauseKeyWord, string likeKeyWord, LikeType likeType)
        {
            this.Init(logicalOperator, field, clauseKeyWord, likeKeyWord, likeType);
        }

        private void Init(LogicalOperator logicalOperator, ModelField field, LikeClauseKeyWord clauseKeyWord, string likeKeyWord, LikeType likeType)
        {
            if (String.IsNullOrEmpty(likeKeyWord))
                throw new Exception("参数likeKeyWord不能为空!");
            this.LogicalOperator = logicalOperator;
            this._modelField = field;
            this._clauseKeyWord = clauseKeyWord;
            this._likeKeyWord = likeKeyWord;
            this._likeType = likeType;
        }

        public string BuildSqlText()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this._modelField.GetDbField().TablePrefixName);

            string likeKeyWrod = this._likeKeyWord;
            DataBaseType dbType = this._modelField.Model.GetDataBase().Type;
            likeKeyWrod = DbHandler.Escape(dbType, likeKeyWrod);

            switch (this._likeType)
            {
                case LikeType.Both:
                    sb.Append(" LIKE '%").Append(likeKeyWrod).Append("%' ");
                    break;
                case LikeType.Left:
                    sb.Append(" LIKE '").Append(likeKeyWrod).Append("%' ");
                    break;
                case LikeType.Right:
                    sb.Append(" LIKE '%").Append(likeKeyWrod).Append("' ");
                    break;
            }
            return sb.ToString();
        }

    }

    public enum LikeClauseKeyWord
    {
        LIKE,
        NOT_LIKE
    }

    public enum LikeType
    {
        Both,
        Left,
        Right
    }

}
